<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'" label-width="140px">
      <el-row >
        <!-- <el-col :span="12">
            <el-form-item label="所属采购" prop="purchase.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择所属采购"
            labelName = 'purchaseName'
            labelValue = 'id'
            v-model = "inputForm.purchase.id"
            :limit="1"
            :columns="[
            {
              prop: 'purchaseNo',
              label: '采购编号'
            },
            {
              prop: 'purchaseName',
              label: '采购名称'
            }
            ]"
            :searchs="[
            {
              prop: 'purchaseNo',
              label: '采购编号'
            },
            {
              prop: 'purchaseName',
              label: '采购名称'
            }
            ]"
            :api="purchaseService">
          </GridSelect>
           </el-form-item>
        </el-col> -->
        <!-- <el-col :span="12">
            <el-form-item label="所属项目" prop="project.id"
                :rules="[{required: true, message:'所属项目不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择所属项目"
            labelName = 'projectName'
            labelValue = 'id'
            v-model = "inputForm.project.id"
            :limit="1"
            :columns="[
            {
              prop: 'projectNo',
              label: '项目编号'
            },
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :searchs="[
            {
              prop: 'projectNo',
              label: '项目编号'
            },
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :api="projectMainService">
          </GridSelect>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="合同名称" prop="contractName"
                :rules="[{required: true, message:'合同名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.contractName" placeholder="请填写合同名称"     ></el-input>
           </el-form-item>
        </el-col>
              <el-col :span="12">
            <el-form-item label="承办部门" prop="executeDepartmentId"
                :rules="[{required: true, message:'承办部门不能为空', trigger:'blur'}
                 ]">
                <office-select v-model="inputForm.executeDepartmentId" type="2"/>
           </el-form-item>
        </el-col>
              <el-col :span="12">
            <el-form-item label="主合同编号" prop="fdMainNumber"
                :rules="[                 ]">
              <el-input v-model="inputForm.fdMainNumber" placeholder="请填写合同编号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同编号" prop="contractNo"
                :rules="[{required: true, message:'合同编号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.contractNo" placeholder="请填写合同编号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="业主/供应商" prop="supplier.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择供应商"
            labelName = 'companyName'
            labelValue = 'id'
            v-model = "inputForm.supplier.id"
            :limit="1"
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
           
            {
              prop: 'companyName',
              label: '单位名称'
            }
            ]"
            :searchs="[
         
            {
              prop: 'companyName',
              label: '单位名称'
            }
            ]"
            :api="correspondentService">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同性质（收入/支出）" prop="contractFeature" label-width="180"
                :rules="[{required: true, message:'合同性质不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.contractFeature" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('contract_feature')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同类型" prop="contractType"
                :rules="[{required: true, message:'合同类型不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.contractType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('contract_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同金额" prop="contractAmount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.contractAmount" placeholder="请填写合同金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="修正后合同金额" prop="correctContractAmount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.correctContractAmount" placeholder="请填写修正后合同金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="税率(%)" prop="taxRate"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taxRate" placeholder="请填写税率"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="税额" prop="taxAmount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taxAmount" placeholder="请填写税额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同签订时间" prop="contractSignTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.contractSignTime"
                      type="datetime"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同生效时间" prop="contractBeginTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.contractBeginTime"
                      type="datetime"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同结束时间" prop="contractEndTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.contractEndTime"
                      type="datetime"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
    
                <el-col :span="12">
            <el-form-item label="是否科研" prop="isScientificResearch"
                :rules="[
                 ]">
                <el-select v-model="inputForm.isScientificResearch" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="质保金" prop="qualityGuaranteeFund"
                :rules="[
                 ]">
                //  {required: true, message:'质保金不能为空', trigger:'blur'}
              <el-input v-model="inputForm.qualityGuaranteeFund" placeholder="请填写质保金"     ></el-input>
           </el-form-item>
        </el-col>
            <el-col :span="12">
            <el-form-item label="合同附件" prop="attachment"
                :rules="[
                 ]">
              <file-upload v-model="inputForm.attachment" tip="单个文件不要超过200M"></file-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="备注" prop="remark"
                :rules="[
                 ]">
              <el-input v-model="inputForm.remark" placeholder="请填写备注"  type="textarea" :rows="6"   ></el-input>
           </el-form-item>
        </el-col>

        </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>


</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import tPurchaseContractService from '@/api/contract/tPurchaseContractService'
  import purchaseService from '@/api/purchase/purchaseService'   
  import projectMainService from '@/api/project/projectMainService'   
  import correspondentService from '@/api/correspondent/correspondentService'   
import OfficeSelect from '@/components/officeSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        purchaseService: purchaseService,  
        projectMainService: projectMainService,  
        correspondentService: correspondentService,  
        inputForm: {
          id: '',
          purchase: {
            id: ''
          },
          project: {
            id: ''
          },
          contractName: '',
          contractNo: '',
          supplier: {
            id: ''
          },
          contractFeature: '',
          contractType: '',
          contractAmount: '',
          correctContractAmount: '',
          taxRate: '',
          taxAmount: '',
          contractSignTime: '',
          contractBeginTime: '',
          contractEndTime: '',
          attachment: '',
          qualityGuaranteeFund: '',
          remark: '',
          isScientificResearch: '',
            fileName:'',
        }
      }
    },
    components: {
      GridSelect,
      OfficeSelect
    },
     watch: {
    'inputForm.attachment': {
      handler(newVal) {
        if (newVal) {
          // 获取所有文件信息
          const files = newVal.split(',');
          const fileNames = [];
          const fileUrls = [];

          files.forEach(file => {
            if (file&&file.indexOf(',')) {
              const urlArr = file.split('&fileName=');
              if (urlArr.length >= 2) {
                const fileName = urlArr[urlArr.length - 1];
                const fileUrl = urlArr[0];
                fileNames.push(fileName);
                fileUrls.push(fileUrl);
              } else {
                return;
              }
            }
          });         
          if (fileNames.length) {
            this.inputForm.fileName = fileNames.join(',');
            // 保存纯文件地址（不带文件名）
            // this.inputForm.attachment = fileUrls.join(',');
          }
        }
      },
      immediate: true
    }
  },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建合同台账`
        } else if (method === 'edit') {
          this.title = '修改合同台账'
        } else if (method === 'view') {
          this.title = '查看合同台账'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            tPurchaseContractService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            tPurchaseContractService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    },
    
  }
</script>

  
